package com.dhemery.mack.adapters.tests;

import static org.junit.Assert.*;

import org.eclipse.core.runtime.IAdapterFactory;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IEditorInput;
import org.junit.Before;
import org.junit.Test;

import com.dhemery.mack.adapters.NodeAdapterFactory;
import com.dhemery.mack.adapters.NodeEditorInput;
import com.dhemery.mack.model.Folder;
import com.dhemery.mack.model.Node;
import com.dhemery.mack.model.Page;
import com.dhemery.mack.model.Project;
import com.dhemery.mack.model.Session;

public class NodeAdapterFactoryIEditorInputTests {
	private FakeConstantImageDescriptorSource fakeSource;
	private ImageDescriptor fakeImageDescriptor;

	@Before
	public void setUp() {
		fakeImageDescriptor = new FakeImageDescriptor("arbitrary image descriptor name");
		fakeSource = new FakeConstantImageDescriptorSource(fakeImageDescriptor);
	}

	@Test
	public void adaptsNodeToNodeEditorInput() {
		IAdapterFactory factory = new NodeAdapterFactory(null);
		Node node = new Node(null);
		Object adapter = factory.getAdapter(node, IEditorInput.class);
		assertEquals(NodeEditorInput.class, adapter.getClass());
	}

	@Test
	public void adaptsPageToNodeEditorInput() {
		IAdapterFactory factory = new NodeAdapterFactory(fakeSource);
		Page page = new Page();
		Object adapter = factory.getAdapter(page, IEditorInput.class);
		assertEquals(NodeEditorInput.class, adapter.getClass());
	}

	@Test
	public void adaptsFolderToNodeEditorInput() {
		IAdapterFactory factory = new NodeAdapterFactory(fakeSource);
		Folder folder = new Folder();
		Object adapter = factory.getAdapter(folder, IEditorInput.class);
		assertEquals(NodeEditorInput.class, adapter.getClass());
	}

	@Test
	public void adaptsProjectToNodeEditorInput() {
		IAdapterFactory factory = new NodeAdapterFactory(fakeSource);
		Project project = new Project();
		Object adapter = factory.getAdapter(project, IEditorInput.class);
		assertEquals(NodeEditorInput.class, adapter.getClass());
	}

	@Test
	public void adaptsSessionToNodeEditorInput() {
		IAdapterFactory factory = new NodeAdapterFactory(fakeSource);
		Session session = new Session();
		Object adapter = factory.getAdapter(session, IEditorInput.class);
		assertEquals(NodeEditorInput.class, adapter.getClass());
	}
	
	@Test
	public void adapterInputIsGivenNode() {
		IAdapterFactory factory = new NodeAdapterFactory(fakeSource);
		Node node = new Node("arbitrary name");
		NodeEditorInput adapter = (NodeEditorInput)factory.getAdapter(node, IEditorInput.class);
		assertSame(node.getName(), adapter.getName());
	}
	
	@Test
	public void adapterImageDescriptorComesFromGivenSource() {
		IAdapterFactory factory = new NodeAdapterFactory(fakeSource);
		NodeEditorInput adapter = (NodeEditorInput)factory.getAdapter(new Project(), IEditorInput.class);
		assertSame(fakeImageDescriptor, adapter.getImageDescriptor());
	}
}
